package com.cq.androidtools.jetpack.paging3

import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import androidx.recyclerview.widget.LinearLayoutManager
import com.cq.androidtools.databinding.ActivityRepoBinding
import com.cq.androidtools.jetpack.databinding.DataBindingActivity
import com.cq.base.utils.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * 作者: CQ
 * 日期: 2021-07-07
 * 说明:
 */
@AndroidEntryPoint
class RepoActivity : DataBindingActivity<ActivityRepoBinding>() {

    @Inject
    lateinit var repoAdapter: RepoAdapter

    private val viewModel: MainViewModel by viewModels()

    private var isRefresh = false
    private var isLoad = false

    @InternalCoroutinesApi
    override fun ActivityRepoBinding.initBinding() {
        mBinding.recyclerView.layoutManager = LinearLayoutManager(this@RepoActivity)
        mBinding.recyclerView.adapter = repoAdapter.withLoadStateFooter(FooterAdapter {
            repoAdapter.retry()
        })
        mBinding.swipeRefreshLayout.setOnRefreshListener { repoAdapter.refresh() }

        lifecycleScope.launch {
            viewModel.getPagingData().collect { pagingData -> repoAdapter.submitData(pagingData) }
        }

        repoAdapter.addLoadStateListener {
            when (it.refresh) {
                is LoadState.NotLoading -> {
                    if (isRefresh) {
                        Log.e("refresh", "LoadState.NotLoading = ${it.refresh}")
                        progressBar.visibility = View.INVISIBLE
                        mBinding.swipeRefreshLayout.isRefreshing = false
                    }
                    isRefresh = false
                }
                is LoadState.Loading -> {
                    isRefresh = true
                    Log.e("refresh", "LoadState.NotLoading = ${it.refresh}")
                    progressBar.visibility = View.VISIBLE
                }
                is LoadState.Error -> {
                    if (isRefresh) {
                        val state = it.refresh as LoadState.Error
                        progressBar.visibility = View.INVISIBLE
                        Log.e("refresh", "LoadState.NotLoading = ${it.refresh}")
                    }
                    isRefresh = false
                }
            }
            when (it.append) {
                is LoadState.NotLoading -> {
                    if (isLoad) {
                        Log.e("append", "LoadState.NotLoading = ${it.append}") //                    progressBar.visibility = View.INVISIBLE
                    }
                    isLoad = false
                }
                is LoadState.Loading -> {
                    isLoad = true
                    Log.e("append", "LoadState.Loading = ${it.append}") //                    progressBar.visibility = View.VISIBLE
                    //                    recyclerView.visibility = View.INVISIBLE
                }
                is LoadState.Error -> {
                    if (isLoad) {
                        Log.e("append", "LoadState.Error = ${it.append}")
                        val state = it.append as LoadState.Error
                        progressBar.visibility = View.INVISIBLE
                        ToastUtils.makeText(this@RepoActivity, "Load Error: ${state.error.message}", Toast.LENGTH_SHORT).show()
                    }
                    isLoad = false
                }
            }
        }
    }
}